package com.sumainfor.pc.dev.service.impl;


import com.sumainfor.common.Constants;
import com.sumainfor.common.util.ContextUtils;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.utlis.ComUtils;
import com.sumainfor.common.utlis.ConvertDateTime;
import com.sumainfor.common.utlis.ParamsCommon;
import com.sumainfor.pc.dev.dao.BannerMapper;
import com.sumainfor.pc.dev.dao.UtilsMapper;
import com.sumainfor.pc.dev.service.BannerService;
import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;

/**
 * <p>
 *首页Banner
 * <p>
 *
 * @author: zhoucong
 * @create: 2019-04-01 18:16
 **/
@Service
public class BannerServiceImpl implements BannerService {

    private ReentrantLock lock = new ReentrantLock(false);
    Logger log = LoggerFactory.getLogger(BannerServiceImpl.class);

    @Autowired
    BannerMapper bannerMapper;

    @Autowired
    UtilsMapper utilsMapper;

    /**
     * <p>
     * 查询上下架banner的数量
     * </p>
     *
     * @author: zhoucong
     * @since:
     */
    @Override
    public Integer getBannerStatusCount(Map<String,Object> params){

        return bannerMapper.getBannerStatusCount(params);
   }

    /**
     * <p>
     * 查询上下架首页Banner
     * </p>
     *
     * @author: zhoucong
     * @since:
     */
   @Override
    public List<Map<String,Object>> getBannerStatus(Map<String,Object> params){
       List<Map<String,Object>> BannerList = bannerMapper.getBannerStatus(params);
//       for (Map<String,Object> BannerMap:BannerList) {
//           if(BannerMap.get("Neturl")!=null){
//               String Neturl = MapUtils.getString(BannerMap,"Neturl");
//               System.out.println("Neturl"+Neturl);
//               String HouInfoId =Neturl.substring(Neturl.lastIndexOf("=")+1);
//               System.out.println("HouInfoId"+HouInfoId);
//               BannerMap.put("HouInfoId",HouInfoId);
//           }
//       }
        return BannerList;
    }

    /**
     * <p>
     * 更新首页Banner上下架状态和编辑Banner
     * </p>
     *
     * @author: zhoucong
     * @since:
     */
   @Override
    public JsonResult updateBannerStatus(Map<String,Object> params){
      JsonResult result = new JsonResult();

       /**
        * 加锁操作
        */
       lock.lock();
       try {
           log.info("更新操作");
           params.put("by", ContextUtils.getUserId());//操作人
           params.put("term","7");//1 租客端 2市场部 3 工程部 4运营部 5财务部 6总经办 7发展部
           params.put("fnc",
                    this.getClass().getSimpleName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName());// 创建Function名
           ParamsCommon.commonUpdateParms(params);
           System.out.println("params"+params);
           if (params.get("BanType")!=null) {
               Integer a = MapUtils.getInteger(params, "BanType");
               if (a == 1) {
                   if (params.get("Neturl") != null) {
                       StringBuffer NeturlOne = new StringBuffer("homeDetail?HouInfoId=");
                       String HouInfoId = MapUtils.getString(params,"Neturl");
                       NeturlOne.append(HouInfoId);
                       String Neturl = NeturlOne.toString();
                       params.put("Neturl",Neturl);
                   }
               }
           }

           if(bannerMapper.updateBannerStatus(params)<=0){
            result.putFailed("修改失败!");
        }else{
            result.putSuccess("修改成功!");
         }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//事务回滚
          result.putFailed("服务器繁忙...");
        } finally {
            lock.unlock();
        }
         return result;
    }


    /**
    * <p>
    * 删除banner
    * </p>
    *
    * @author: zhoucong
    * @since:
    */
    @Override
    public JsonResult deleteBanner(Map<String,Object> params){
        JsonResult result = new JsonResult();

        /**
         * 加锁操作
         */
        lock.lock();
        try {
            log.info("删除操作");
            if(bannerMapper.deleteBanner(params)<=0){
                result.putFailed("删除失败!");
            }else{
                result.putSuccess("删除成功!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//事务回滚
            result.putFailed("服务器繁忙...");
        } finally {
            lock.unlock();
        }
        return result;

    }




    /**
    * <p>
    * 增加banner
    * </p>
    *
    * @author: zhoucong
    * @since:
    */
    @Override
    public JsonResult insertBanner(Map<String,Object> params){
       JsonResult result = new JsonResult();
        /**
         * 加锁操作
         */
        lock.lock();
        try {
            log.info("增加操作");
            params.put("term", "7");//创建端  1 租客端 2市场部 3 工程部 4运营部 5财务部 6总经办 7发展部
            params.put("fnc",
                    this.getClass().getSimpleName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName());// 创建Function名
            ParamsCommon.commonInsertParms(params);

            String UUID;
            Map<String,Object>uuid=utilsMapper.getUUID(new HashMap<String,Object>(){{
                put("uuid", "BanId");
                put("database", Constants.DATA_SOURCE_TCDP);
                put("surface", "tcdp_banner");
            }});
            if(uuid!=null){
                int number=Integer.valueOf(uuid.get("uuid").toString());
                UUID= ComUtils.getLocalTrmSeqNum("Banner",number);
            }else{
                UUID=ComUtils.getLocalTrmSeqNum("Banner",0);
            }
            params.put("BanId",UUID);
            Integer a =MapUtils.getInteger(params,"BanType");
            if (a ==1){
                StringBuffer NeturlOne = new StringBuffer("homeDetail?HouInfoId=");
                String HouInfoId = MapUtils.getString(params,"Neturl");
                NeturlOne.append(HouInfoId);
                String Neturl = NeturlOne.toString();
                params.put("Neturl",Neturl);
            }
            params.put("CreationTime", ConvertDateTime.getCurrentTime());
            Integer cout = bannerMapper.insertBanner(params);
            if(cout<=0){
                result.putFailed("创建失败!");
            }else{
                result.putSuccess("创建成功!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//事物回滚
            result.putFailed("服务器繁忙...");
        } finally {
            lock.unlock();
        }
   return result;

    }



}

